﻿using AutoMapper;
using HRSolutionSystem.Application.Contracts.Persistence;
using HRSolutionSystem.Application.Exceptions;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRSolutionSystem.Application.Features.LeaveType.Commands.DeleteLeaveType
{
    public class DeleteLeaveTypeCommandHandler : IRequestHandler<DeleteLeaveTypeCommand, MediatR.Unit>
    {
        //private readonly IMapper _mapper;
        private readonly ILeaveTypeRepository _leaveTypeRepository;

        public DeleteLeaveTypeCommandHandler(/*IMapper mapper,*/ ILeaveTypeRepository leaveTypeRepository)
        {
            //this._mapper = mapper;
            this._leaveTypeRepository = leaveTypeRepository;
        }
        public async Task<Unit> Handle(DeleteLeaveTypeCommand request, CancellationToken cancellationToken)
        {

            //retrieve domain entity object
            var leaveTypeToDelete = await _leaveTypeRepository.GetByIdAsync(request.Id);
            //validate that record exists
            if (leaveTypeToDelete == null)
            {
                throw new NotFoundException(nameof(leaveTypeToDelete),request.Id);
            }
            //remove from database
            await _leaveTypeRepository.DeleteAsync(leaveTypeToDelete);

            //return void
            return MediatR.Unit.Value;
        }
    }
}
